#!/sbin/sh

# AnyKernel2 Backend Script
#
# Original and credits: osm0sis @ xda-developers
#
# Modified by Nimit Mehta

OUTFD=/proc/self/fd/$2;
ZIP="$3";
DIR=`dirname "$ZIP"`;


## internal functions

ui_print() 
{
	until [ ! "$1" ]; do
		echo -e "ui_print $1\nui_print" > $OUTFD;
		shift;
	done;
}

show_progress() 
{
	echo "progress $1 $2" > $OUTFD;
}

set_perm_recursive() 
{
	dirs=$(echo $* | $bb awk '{ print substr($0, index($0,$5)) }');
	
	for i in $dirs; do
		chown -R $1.$2 $i; chown -R $1:$2 $i;
		find "$i" -type d -exec chmod $3 {} +;
		find "$i" -type f -exec chmod $4 {} +;
	done;
}

file_getprop() 
{ 
	grep "^$2" "$1" | cut -d= -f2;
}

getprop()
{ 
	test -e /sbin/getprop && /sbin/getprop $1 || file_getprop /default.prop $1;
}

abort()
{ 
	ui_print " ";
	ui_print "$*";
	umount /system;
	umount /data;
	exit 1;
}


## start of main script

show_progress 1.34 4;
ui_print " ";

mkdir -p /tmp/anykernel/bin;
cd /tmp/anykernel;
unzip -o "$ZIP";

if [ $? != 0 -o -z "$(ls /tmp/anykernel/tools)" ]; then
	abort "Error - unzip failed. Aborting.";
fi;

chmod -R 755 /tmp/anykernel/tools /tmp/anykernel/bin;
bb=/tmp/anykernel/tools/busybox;

ui_print "### Arsenic Kernel ###";
ui_print " ";
ui_print "### By-Nimit Mehta ###";
ui_print " ";

mount -o ro /system;
mount /data;

if [ "$(file_getprop /tmp/anykernel/anykernel.sh do.devicecheck)" == 1 ]; then
	ui_print "* Checking device compatibility...";
	ui_print "  - Detected product.device: $(getprop ro.product.device)";
	ui_print "  - Detected build.product : $(getprop ro.build.product)";

	for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
		testname="$(file_getprop /tmp/anykernel/anykernel.sh device.name$i)";
		if [ "$(getprop ro.product.device)" == "$testname" -o "$(getprop ro.build.product)" == "$testname" ]; then
			match=1;
		fi;
	done;

	if [ "$match" != 1 ]; then
		abort "This kernel is NOT compatible with your device. Aborting.";
	else
		ui_print "  Kernel is compatible.";
	fi;

	ui_print " ";
fi;

if [ "$(file_getprop /tmp/anykernel/anykernel.sh do.initd)" == 1 ]; then
	ui_print "* Creating init.d...";
	ui_print " ";

	mount -o remount,rw /system;
	if [ ! -d /system/etc/init.d ]; then
		mkdir -p /system/etc/init.d;
	fi;
	set_perm_recursive 0 0 0755 0755 /system/etc/init.d;
	mount -o remount,ro /system;
fi;

ui_print "* Installing kernel...";
ui_print " ";

for i in $($bb --list); do
	$bb ln -s $bb /tmp/anykernel/bin/$i;
done;

if [ $? != 0 -o -z "$(ls /tmp/anykernel/bin)" ]; then
	abort "Recovery busybox setup failed. Aborting.";
fi;

PATH="/tmp/anykernel/bin:$PATH" $bb ash /tmp/anykernel/anykernel.sh $2;

if [ $? != "0" ]; then
	abort;
fi;

ui_print "* Checking for ROM Compatibility...";
ui_print " ";
ENGINE="/system/bin/thermal-engine";
ENGINEVENDOR="/system/vendor/bin/thermal-engine";
if [ ! -e "$ENGINE" ]; then
	if [ ! -e "$ENGINEVENDOR" ]; then
		ui_print "    Sultanized ROM detected! Adding proper support...";
		mount -o remount,rw /system;
		cp -f /tmp/anykernel/modules/bin/thermal-engine /system/bin/;
		cp -f /tmp/anykernel/modules/bin/mpdecision /system/bin/;
		cp -f /tmp/anykernel/modules/vendor/lib/libthermalclient.so /system/vendor/lib/;
		cp -f /tmp/anykernel/modules/vendor/lib/libthermalioctl.so /system/vendor/lib/;
		cp -f /tmp/anykernel/modules/etc/thermal-engine-8974.conf /system/etc/;
		chown root:shell /system/bin/thermal-engine;
		chmod 755 /system/bin/thermal-engine;
		chown root:shell /system/bin/mpdecision;
		chmod 755 /system/bin/mpdecision;
		chown root:root /system/vendor/lib/libthermalclient.so;
		chmod 644 /system/vendor/lib/libthermalclient.so;
		chown root:root /system/vendor/lib/libthermalioctl.so;
		chmod 644 /system/vendor/lib/libthermalioctl.so;
		chown root:root /system/etc/thermal-engine-8974.conf
		chmod 644 /system/etc/thermal-engine-8974.conf
		mount -o remount,ro /system;
	else
		ui_print "Running  CM14..? Its supported already..! ";
	fi;
else
	ui_print " ROM is supported by default..! ";
fi;
ui_print "  ";

mount -o remount,rw /system
mkdir /system/su.d
echo "#!/system/bin/sh" > /system/su.d/permissive.sh
echo "setenforce 0" > /system/su.d/permissive.sh
echo "0" > /sys/fs/selinux/enforce
chmod 755 /system/su.d/permissive.sh

ui_print "   DONE..!";

if [ "$(file_getprop /tmp/anykernel/anykernel.sh do.cleanup)" == 1 ]; then
	rm -rf /tmp/anykernel;
fi;

umount /system;
umount /data;
ui_print "* Hey flashaholic, the kernel is installed now.";
ui_print "  ... Why are you still here?";
ui_print "  Uhmmm... Reboot?";
ui_print " ";
